home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / appprnt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  5.0 KB  |  203 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #ifndef _INC_CDERR
  13. #include <cderr.h>      // Commdlg Error definitions
  14. #endif
  15. #ifndef _WINSPOOL_
  16. #include <winspool.h>
  17. #endif
  18.  
  19. #if !defined(_WIN32_WCE_NO_PRINTING)
  20. #ifdef AFX_PRINT_SEG
  21. #pragma code_seg(AFX_PRINT_SEG)
  22. #endif
  23.  
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // WinApp support for printing
  31.  
  32. #if !defined(_WIN32_WCE)
  33. BOOL CWinApp::GetPrinterDeviceDefaults(PRINTDLG* pPrintDlg)
  34. {
  35.     UpdatePrinterSelection(m_hDevNames == NULL); //force default if no current
  36.     if (m_hDevNames == NULL)
  37.         return FALSE;               // no printer defaults
  38.  
  39.     pPrintDlg->hDevNames = m_hDevNames;
  40.     pPrintDlg->hDevMode = m_hDevMode;
  41.  
  42.     ::GlobalUnlock(m_hDevNames);
  43.     ::GlobalUnlock(m_hDevMode);
  44.     return TRUE;
  45. }
  46.  
  47. void CWinApp::UpdatePrinterSelection(BOOL bForceDefaults)
  48. {
  49.     if (!bForceDefaults && m_hDevNames != NULL)
  50.     {
  51.         LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  52.         ASSERT(lpDevNames != NULL);
  53.         if (lpDevNames->wDefault & DN_DEFAULTPRN)
  54.         {
  55.             CPrintDialog pd(TRUE);
  56.             pd.GetDefaults();
  57.  
  58.             if (pd.m_pd.hDevNames == NULL)
  59.             {
  60.                 // Printer was default, but now there are no printers at all!
  61.                 if (m_hDevMode != NULL)
  62.                     AfxGlobalFree(m_hDevMode);
  63.                 AfxGlobalFree(m_hDevNames);
  64.                 m_hDevMode = NULL;
  65.                 m_hDevNames = NULL;
  66.             }
  67.             else if (
  68.                 lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  69.                     pd.GetDriverName()) != 0 ||
  70.                 lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  71.                     pd.GetDeviceName()) != 0 ||
  72.                 lstrcmp((LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  73.                     pd.GetPortName()) != 0)
  74.             {
  75.                 // Printer was default, and default has changed...assume default
  76.                 if (m_hDevMode != NULL)
  77.                     AfxGlobalFree(m_hDevMode);
  78.                 AfxGlobalFree(m_hDevNames);
  79.                 m_hDevMode = pd.m_pd.hDevMode;
  80.                 m_hDevNames = pd.m_pd.hDevNames;
  81.             }
  82.             else
  83.             {
  84.                 // Printer was default, and still is...keep the same
  85.                 if (pd.m_pd.hDevMode != NULL)
  86.                     AfxGlobalFree(pd.m_pd.hDevMode);
  87.                 if (pd.m_pd.hDevNames != NULL)
  88.                     AfxGlobalFree(pd.m_pd.hDevNames);
  89.             }
  90.         }
  91.     }
  92.     else
  93.     {
  94.         // First time or Forced -- Get defaults
  95.         CPrintDialog pd(TRUE);
  96.         pd.GetDefaults();
  97.  
  98.         if (m_hDevMode != NULL)
  99.             AfxGlobalFree(m_hDevMode);
  100.         if (m_hDevNames != NULL)
  101.             AfxGlobalFree(m_hDevNames);
  102.  
  103.         m_hDevMode = pd.m_pd.hDevMode;
  104.         m_hDevNames = pd.m_pd.hDevNames;
  105.     }
  106. }
  107. #endif // _WIN32_WCE
  108.  
  109. int CWinApp::DoPrintDialog(CPrintDialog* pPD)
  110. {
  111. #if !defined(_WIN32_WCE)
  112.     UpdatePrinterSelection(FALSE);
  113.  
  114.     pPD->m_pd.hDevMode = m_hDevMode;
  115.     pPD->m_pd.hDevNames = m_hDevNames;
  116. #endif // _WIN32_WCE
  117.     int nResponse = pPD->DoModal();
  118.  
  119.     // if OK or Cancel is selected we need to update cached devMode/Names
  120.     while (nResponse != IDOK && nResponse != IDCANCEL)
  121.     {
  122.         switch (::CommDlgExtendedError())
  123.         {
  124.         // CommDlg cannot give these errors after NULLing these handles
  125.         case PDERR_PRINTERNOTFOUND:
  126.         case PDERR_DNDMMISMATCH:
  127.         case PDERR_DEFAULTDIFFERENT:
  128. #if !defined(_WIN32_WCE)
  129.             if (pPD->m_pd.hDevNames != NULL)
  130.             {
  131.                 ASSERT(m_hDevNames == pPD->m_pd.hDevNames);
  132.                 AfxGlobalFree(pPD->m_pd.hDevNames);
  133.                 pPD->m_pd.hDevNames = NULL;
  134.                 m_hDevNames = NULL;
  135.             }
  136.  
  137.             if (pPD->m_pd.hDevMode)
  138.             {
  139.                 ASSERT(m_hDevMode == pPD->m_pd.hDevMode);
  140.                 AfxGlobalFree(pPD->m_pd.hDevMode);
  141.                 pPD->m_pd.hDevMode = NULL;
  142.                 m_hDevMode = NULL;
  143.             }
  144. #endif // _WIN32_WCE
  145.             break;
  146.  
  147.         default:
  148.             return nResponse;       // do not update cached devMode/Names
  149.         }
  150.  
  151.         nResponse = pPD->DoModal();
  152.     }
  153.  
  154.     // refresh current CWinApp cache of printer device information
  155. #if defined(_WIN32_WCE)
  156.     m_hdc = pPD->m_pd.hdc;
  157. #else // _WIN32_WCE
  158.     m_hDevMode = pPD->m_pd.hDevMode;
  159.     m_hDevNames = pPD->m_pd.hDevNames;
  160. #endif // _WIN32_WCE
  161.  
  162.     return nResponse;
  163. }
  164.  
  165. #if !defined(_WIN32_WCE)
  166. void CWinApp::OnFilePrintSetup()
  167. {
  168.     CPrintDialog pd(TRUE);
  169.     DoPrintDialog(&pd);
  170. }
  171.  
  172. void CWinApp::SelectPrinter(HANDLE hDevNames, HANDLE hDevMode, BOOL bFreeOld)
  173. {
  174.     if (m_hDevNames != hDevNames)
  175.     {
  176.         if (m_hDevNames != NULL && bFreeOld)
  177.             AfxGlobalFree(m_hDevNames);
  178.         m_hDevNames = hDevNames;
  179.     }
  180.     if (m_hDevMode != hDevMode)
  181.     {
  182.         if (m_hDevMode != NULL && bFreeOld)
  183.             AfxGlobalFree(m_hDevMode);
  184.         m_hDevMode = hDevMode;
  185.     }
  186. }
  187.  
  188. BOOL CWinApp::CreatePrinterDC(CDC& dc)
  189. {
  190.     HDC hDC = AfxCreateDC(m_hDevNames, m_hDevMode);
  191.     if (hDC != NULL)
  192.     {
  193.         dc.DeleteDC();
  194.         VERIFY(dc.Attach(hDC));
  195.         return TRUE;
  196.     }
  197.     return FALSE;
  198. }
  199. #endif // _WIN32_WCE
  200. #endif // _WIN32_WCE_NO_PRINTING
  201.  
  202. /////////////////////////////////////////////////////////////////////////////
  203.